package br.fagoc.easyds.datastructure;

import br.fagoc.easyds.exception.EmptyStructureException;
import br.fagoc.easyds.exception.FullStructureException;
import br.fagoc.easyds.ui.code.TextAreaController;
import br.fagoc.easyds.ui.jgraph.GraphCellManipulator;
import br.fagoc.easyds.ui.jgraph.GraphEdgeManipulator;
import br.fagoc.easyds.ui.jgraph.GraphManipulator;

import com.mxgraph.model.mxCell;
import com.mxgraph.view.mxGraph;

public class StaticQueue {
	private final double CELULA_LARGURA = 80;
	private final double CELULA_ALTURA = 50;
	private final double EIXO_X_ORIGEM = 0;
	private final double EIXO_Y_ORIGEM = 100;
	private final double EIXO_X_DESTINO = 20;
	private final double EIXO_Y_DESTINO = 20;
	private final double EIXO_X_LIMITE_REMOCAO = 0 - CELULA_LARGURA;
	private final double EIXO_Y_LIMITE_REMOCAO = 100;
	private final double MODIFICADOR_EIXO_X = 82;

	private GraphManipulator graphManipulator;
	private GraphCellManipulator graphCellManipulator;
	private GraphEdgeManipulator graphEdgeManipulator;
	
	private mxCell[] queue;
	private int maximumLength;
	private int length = 0;
	
	private TextAreaController taController;
	
	public StaticQueue(mxGraph graph, TextAreaController taController, int size){
		this.graphManipulator = new GraphManipulator(graph);
		this.graphCellManipulator = new GraphCellManipulator(graph);
		this.graphEdgeManipulator = new GraphEdgeManipulator(graph);

		queue = new mxCell[size];
		this.maximumLength = size;
		
		this.taController = taController;
		initializeStructure();
	}
	
	public void initializeStructure(){
		for(int i = 0; i < maximumLength; i++){
			queue[i] = graphCellManipulator.drawRectangularCell("", EIXO_X_DESTINO + (i * MODIFICADOR_EIXO_X), EIXO_Y_DESTINO);
		}
		
		taController.showText("/br/fagoc/easyds/file/FilaEstatica.txt");
	}
	
	public void enqueue(String value) throws FullStructureException{
		if(length == maximumLength){
			throw new FullStructureException();
		}
		else{
			mxCell cell = queue[0];
			graphCellManipulator.blinkCell(cell);
			
			for(int i = length-1; i >= 0; i--){
				queue[i+1].setValue(queue[i].getValue().toString());
			}
			
			cell.setValue(value);
			length++;
			
			for(int i = 0; i < length; i++){
				graphCellManipulator.drawCell(queue[i]);
			}
		}
	}
	
	public void dequeue() throws EmptyStructureException{
		if(length == 0){
			throw new EmptyStructureException();
		}
		else{
			mxCell cell = queue[0];
			graphCellManipulator.blinkCell(cell);
			
			for(int i = 0; i < length-1; i++){
				queue[i].setValue(queue[i+1].getValue().toString());
			}
			
			queue[length-1].setValue("");
			length--;
			
			for(int i = 0; i < maximumLength; i++){
				graphCellManipulator.drawCell(queue[i]);
			}
		}
	}
	
	public void peek() throws EmptyStructureException{
		if(length == 0){
			throw new EmptyStructureException();
		}
		else{
			mxCell cell = queue[0];
			graphCellManipulator.blinkCell(cell);
		}
	}
}
